<template>
  <div class="DayPayment overflowY" v-loading="loading">
    <!--<div class="PageTitle"><i class="iconfont icon-jia"></i>费用日结交账</div>-->
    <div class="PageContent">
      <el-form :model="searchData" :inline="true" ref="ruleForm" label-width="120px" class="demo-ruleForm">
        <div class="PageItem margintop20">
          <el-row :gutter="5">
            <el-form-item label="结算日期:">
              <el-date-picker v-model="searchData.settleTimeStart" @change="changeStart" format="yyyy-MM-dd HH:mm"
                              type="datetime" placeholder="选择日期时间"></el-date-picker>
              -
              <el-date-picker v-model="searchData.settleTimeEnd" @change="changeEnd" format="yyyy-MM-dd HH:mm"
                              type="datetime" placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
            <el-form-item label="收款人:">
              <el-input v-model="searchData.settleUserName" :maxlength="20"></el-input>
            </el-form-item>
            <el-form-item label="发票号段:">
              <el-input v-model="searchData.invoiceNoSegment" :maxlength="20"></el-input>
            </el-form-item>
            <el-form-item label="日结状态:">
              <el-select v-model="searchData.verifyState" placeholder="请选择">
                <el-option label="全部" value="全部"></el-option>
                <el-option label="未审核" value="0"></el-option>
                <el-option label="已审核" value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" v-if="btnRight.btnDayPaymentSearch" @click="ReadData">查询</el-button>
            </el-form-item>
          </el-row>
          <el-row :gutter="5">
            <el-form-item label="日结单号:">
              <el-input v-model="searchData.verifyBatchNo" :maxlength="20"></el-input>
            </el-form-item>
            <el-form-item label="日结审核人:">
              <el-input v-model="searchData.verifyUserName" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" plain v-if="btnRight.btnDayPaymentDayCheck" @click="dailyexpensesverify">日结审核
              </el-button>
            </el-form-item>
          </el-row>
        </div>
        <div class="PageItem marginbottom0">
          <el-table
            :data="dailyData.chargeSettles"
            :header-cell-style="{background:'rgb(246,247,251)',color:'#606266'}"
            border
            stripe
            style="width: 100%;"
            @selection-change="handleSelectionChange">
            <el-table-column
              type="selection"
              :selectable="selectable"
              width="50">
            </el-table-column>
            <el-table-column align='center' prop="businessCode" label="业务编码" style="min-width:150px;">
            </el-table-column>
            <el-table-column
              prop="deadName"
              align='center'
              label="逝者姓名"
              width="100">
            </el-table-column>
            <el-table-column
              prop="settleType"
              align='center'
              label="结算项目"
              width="100">
            </el-table-column>
            <el-table-column
              prop="settleTime"
              align='center'
              label="结算时间"
              width="150">
            </el-table-column>
            <el-table-column
              prop="settleCharge"
              align='right'
              label="结算金额"
              width="120">
            </el-table-column>
            <el-table-column
              prop="payType"
              align='center'
              label="付款方式"
              width="100">
            </el-table-column>
            <el-table-column
              prop="invoiceNo"
              align='center'
              label="发票代号"
              style="min-width:200px;">
            </el-table-column>
            <el-table-column
              prop="receiptNo"
              align='center'
              label="收据号"
              style="min-width:200px;">
            </el-table-column>
            <el-table-column
              prop="settleUserName"
              align='center'
              label="收款人"
              width="100">
            </el-table-column>
            <el-table-column
              prop="zip"
              align='center'
              label="日结审核"
              width="100">
              <template slot-scope="{row,$index}">
                <span v-if="row.asVerify==0">未审核</span>
                <span v-else-if="row.asVerify==1">已审核</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="verifyUserName"
              align='center'
              label="审核人"
              width="100">
            </el-table-column>
            <el-table-column
              prop="verifyTime"
              align='center'
              label="审核日期"
              style="min-width:150px;">
            </el-table-column>
          </el-table>
        </div>
        <p style="margin-top: 20px;"></p>
        <div class="PageItem marginbottom0">
          <el-row class="PageInfo"><i class="info-bar"></i>日结审核历史记录</el-row>
          <el-table
            :data="dailyData.chargeVerifies"
            :header-cell-style="{background:'rgb(246,247,251)',color:'#606266'}"
            border
            stripe
            style="width: 100%;"
            @selection-change="handleSelectionChangeExpenses">
            <!--<el-table-column-->
            <!--type="selection"-->
            <!--width="50">-->
            <!--</el-table-column>-->
            <el-table-column align='center' prop="id" label="日结时间" width="200">
            </el-table-column>
            <el-table-column
              prop="verifyBatchNo"
              align='center'
              label="日结单号"
              width="250">
            </el-table-column>
            <el-table-column
              prop="verifyCharge"
              align='center'
              label="日结金额"
              width="200">
            </el-table-column>
            <el-table-column
              prop="invoiceCount"
              align='center'
              label="发票数量"
              width="200">
            </el-table-column>
            <el-table-column
              prop="receiptCount"
              align='center'
              label="收据数量"
              width="200">
            </el-table-column>
            <el-table-column
              prop="verifyUserName"
              align='center'
              label="日结审核人"
              width="200">
            </el-table-column>
            <el-table-column
              label="操作"
              align='center'
              width="300px">
              <template slot-scope="{row,$index}">
                <el-button
                  size="small"
                  type="text"
                  @click.native.prevent="cancellation($index, row)">撤销日结审核
                </el-button>
                <el-button
                  size="small"
                  type="text"
                  @click.native.prevent="handleEdit($index, row)">打印日结单
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {dataFormate, dataFormatetmd} from '@/utils/index'
import * as Type from '@/api/service'

export default {
  name: "DayPayment",
  data() {
    return {
      loading: false,
      searchData: {
        "settleTimeStart": "",
        "settleTimeEnd": "",
        "settleUserName": "",
        "invoiceNoSegment": "",
        "verifyState": "全部",
        "receiptNoSegment": "",
        "verifyBatchNo": "",
        "verifyUserName": this.$store.getters.userInfo.fullName
      },
      dailyData: {
        "chargeSettles": [],
        "chargeVerifies": []
      },
      multipleSelection: [],
      multipleSelectionExpenses: [],
      //按钮权限
      btnRight: {
        btnDayPaymentSearch: true,  //查询
        btnDayPaymentDayCheck: true,//日结审核
      }
    }
  }, mounted() {
    //按钮权限
    this.btnRight = this.$route.meta.buttons;
    //遍历属性赋值将true与false字符串转换成布尔型的值
    for (let key of Object.keys(this.btnRight)) {
      this.btnRight[key] = (this.btnRight[key] || this.btnRight[key] == "true") ? true : false;
    }
    this.ReadData()
  },
  methods: {
    //数据读取
    ReadData() {
      let postData = JSON.parse(JSON.stringify(this.searchData))
      postData.settleTimeStart = dataFormate(new Date(postData.settleTimeStart))
      postData.settleTimeEnd = dataFormate(new Date(postData.settleTimeEnd))
      //结算人员名称(收款人)
      postData.settleUserName = this.searchData.settleUserName
      //发票号片段
      postData.invoiceNoSegment = this.searchData.invoiceNoSegment
      //日结审核批次编号段
      postData.verifyBatchNo = this.searchData.verifyBatchNo
      // console.info(postData);
      // postData.settleUserName = this.$store.getters.userInfo.fullName
      postData.verifyUserId = ""
      // postData.verifyUserId = this.$store.getters.userInfo.userId
      if (this.searchData.verifyState == "全部") {
        postData.verifyState = null
      }
      this.loading = true
      Type['loadsettledailyexpensesall'](postData).then(response => {
        const {data} = response;
        if (!data) {
          this.loading = false;
          this.$message('数据读取失败')
        }
        this.dailyData = data;
        // console.log(data)
        this.loading = false
      }).catch(error => {
        this.$message(error)
        this.loading = false
      })
    },
    //日结审核
    dailyexpensesverify() {
      if (this.multipleSelection.length <= 0) {
        this.$message("未选中审核项")
        return
      }
      let ids = [], charge = 0
      this.multipleSelection.forEach(item => {
        if (item.id) {
          let obj = {}
          obj.id = item.id
          obj.version = item.version
          ids.push(obj)
          charge += Number(item.settleCharge)
        }
      })
      let postData = {
        "dailyCharge": charge,
        "invoiceCount": this.multipleSelection.length,
        "receiptCount": this.multipleSelection.length,
        "ids": ids,
      }
      Type['savesettledailyexpensesverify'](postData).then(response => {
        this.ReadData()
      }).catch(error => {
        this.$message(error)
      })
    },
    //设置是否选中
    selectable(row, index) {
      if (row.asVerify == 1) {
        return false
      } else {
        return true
      }

    },

    //撤销审核
    cancellation(index, row) {
      this.$confirm('撤销日结审核, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //点击确定的操作(调用接口)
        let postData = [{id: row.id, version: row.version}];
        Type['savesettledailyexpensescancellationverify'](postData).then(response => {
          this.$message(response.msg)
          this.ReadData()
        }).catch(error => {
          this.$message(error)
        })
      }).catch(() => {
        //几点取消的提示
      });

    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSelectionChangeExpenses(val) {
      this.multipleSelectionExpenses = val;

    },//清空开始时间事件
    changeStart(val) {
      if (val == null) {
        this.searchData.settleTimeStart = "";
      }

    }, changeEnd(val) {
      if (val == null) {
        this.searchData.settleTimeEnd = "";
      }

    }
  }
}
</script>

<style scoped>
.DayPayment .PageContent {
  height: 100vh;
  min-height: 100vh !important;
}
</style>
